package com.demo.controller;

import com.demo.common.Result;
import com.demo.mapper.*;
import com.demo.pojo.entity.*;
import com.demo.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 排课数据测试控制器
 */
@RestController
@RequestMapping("/schedule-test")
@CrossOrigin
public class ScheduleDataTestController {
    
    @Autowired
    private ScheduleService scheduleService;
    
    @Autowired
    private CourseMapper courseMapper;
    
    @Autowired
    private TeacherMapper teacherMapper;
    
    @Autowired
    private ClassroomMapper classroomMapper;
    
    @Autowired
    private ClazzMapper clazzMapper;
    
    @Autowired
    private TimeSlotMapper timeSlotMapper;
    
    /**
     * 测试数据库连接和数据
     */
    @GetMapping("/database-status")
    public Result<String> testDatabaseStatus() {
        try {
            StringBuilder status = new StringBuilder();
            
            // 检查课程数据
            List<Course> courses = courseMapper.selectList(null);
            status.append("课程数量: ").append(courses.size()).append("\n");
            
            // 检查教师数据
            List<Teacher> teachers = teacherMapper.selectList(null);
            status.append("教师数量: ").append(teachers.size()).append("\n");
            
            // 检查教室数据
            List<Classroom> classrooms = classroomMapper.selectList(null);
            status.append("教室数量: ").append(classrooms.size()).append("\n");
            
            // 检查班级数据
            List<Clazz> clazzes = clazzMapper.selectList(null);
            status.append("班级数量: ").append(clazzes.size()).append("\n");
            
            // 检查时间段数据
            List<TimeSlot> timeSlots = timeSlotMapper.selectList(null);
            status.append("时间段数量: ").append(timeSlots.size()).append("\n");
            
            // 检查排课数据
            List<Schedule> schedules = scheduleService.list();
            status.append("排课数量: ").append(schedules.size()).append("\n");
            
            return Result.success(status.toString());
            
        } catch (Exception e) {
            return Result.error("数据库状态检查失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试排课详情数据
     */
    @GetMapping("/schedule-details")
    public Result<String> testScheduleDetails() {
        try {
            List<Schedule> schedules = scheduleService.list();
            if (schedules.isEmpty()) {
                return Result.success("没有排课数据");
            }
            
            StringBuilder details = new StringBuilder();
            details.append("排课详情测试:\n");
            
            for (int i = 0; i < Math.min(3, schedules.size()); i++) {
                Schedule schedule = schedules.get(i);
                details.append("排课ID: ").append(schedule.getScheduleId()).append("\n");
                details.append("课程ID: ").append(schedule.getCourseId()).append("\n");
                details.append("教师ID: ").append(schedule.getTeacherId()).append("\n");
                details.append("教室ID: ").append(schedule.getClassroomId()).append("\n");
                details.append("班级ID: ").append(schedule.getClazzId()).append("\n");
                details.append("时间段ID: ").append(schedule.getTimeSlotId()).append("\n");
                details.append("---\n");
            }
            
            return Result.success(details.toString());
            
        } catch (Exception e) {
            return Result.error("排课详情测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试排课列表DTO数据
     */
    @GetMapping("/schedule-list-dto")
    public Result<String> testScheduleListDTO() {
        try {
            List<com.demo.pojo.dto.ScheduleListDTO> scheduleList = scheduleService.getScheduleList();
            if (scheduleList.isEmpty()) {
                return Result.success("没有排课列表数据");
            }
            
            StringBuilder details = new StringBuilder();
            details.append("排课列表DTO测试:\n");
            
            for (int i = 0; i < Math.min(3, scheduleList.size()); i++) {
                com.demo.pojo.dto.ScheduleListDTO dto = scheduleList.get(i);
                details.append("排课ID: ").append(dto.getScheduleId()).append("\n");
                details.append("课程名称: ").append(dto.getCourseName()).append("\n");
                details.append("教师姓名: ").append(dto.getTeacherName()).append("\n");
                details.append("教室名称: ").append(dto.getClassroomName()).append("\n");
                details.append("班级名称: ").append(dto.getClazzName()).append("\n");
                details.append("学期: ").append(dto.getSemester()).append("\n");
                details.append("周次: ").append(dto.getWeekNo()).append("\n");
                details.append("星期: ").append(dto.getDayOfWeek()).append("\n");
                details.append("节次: ").append(dto.getPeriodNo()).append("\n");
                details.append("---\n");
            }
            
            return Result.success(details.toString());
            
        } catch (Exception e) {
            return Result.error("排课列表DTO测试失败: " + e.getMessage());
        }
    }
}
























